package com.test.ssm.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 徒有琴
 */
@WebFilter("*.html")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String path = request.getServletPath();
        System.out.println(path);
        if ("/login.html".equals(path) || "/dologin.html".equals(path)) {
            filterChain.doFilter(request, response);
            return;
        }
        if (request.getSession().getAttribute("session_user") == null) {
            response.sendRedirect("login.html");
            return;
        }
        filterChain.doFilter(request, response);
    }
}
